<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <div id="app">
        <!-- <div class="box">
            <span>1111</span>
            span
            span    
        </div> -->
    </div>
    <script>
        // var obj = {
        //     name:'zhansgan'
        // }
        // console.log(obj["name"]);
        // 浏览器只认识html，css ，js ，
        // jsx他不认识， vue不认识
        // 虚拟dom是一个对象，是用来描述dom节点的js对象
        // 频繁操作dom的成本高，所以我们通过操作对象来操作dom
        var app1 = {
            type: "div",
            props: {
                class: "box",
                children: [{
                    type: "span",
                    props: {
                        children: ["111"]
                    }
                }]
            }
        }

        function render(jsx, root) { // 将虚拟dom渲染在页面上的函数
            let createElement = (jsx, parent) => { // 创建dom节点的方法
                let cur = null; // 用cur这个变量来承接创键出来的dom节点
                let {
                    type,
                    props
                } = jsx
                if (typeof (jsx) == "string") {
                    cur = document.createTextNode(jsx)
                    parent.append(cur)
                } else {
                    cur = document.createElement(type)
                    parent.append(cur)
                    console.log(props)
                    console.log(Object.keys(props))
                    Object.keys(props).forEach((item, index) => {
                        if (item == "class") {
                            cur.setAttribute('class', props[item])
                        } else if (item == "children") {
                            if (Array.isArray(props[item])) {
                                props[item].map((val) => {
                                    createElement(val, cur)
                                })
                            }
                        }
                    })
                }


            }
            createElement(jsx, root)

        }
        render(app1, app)
    </script>
</body>

</html>